﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc。@Copy Right 2019
// 文件： GovLogSafe.cs
// 项目名称：斑马车务
// 创建时间：2019-01-09
// 负责人：lsc
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using Dapper.Extensions;
using CGT.Infrastructure.Entity;
using CGT.Infrastructure.Repository;
using OF.Utility;
using System.Text;
using CGT.Infrastructure.Dto;

namespace CGT.Infrastructure.Service
{

    /// <summary>
    ///
    /// </summary>
    public class GovLogSafeService
    {
        protected GovLogSafeRepository repository;

        public GovLogSafeService()
        {
            repository = new GovLogSafeRepository();
        }

        #region 增删改查

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(GovLogSafe entity)
        {
            entity.CheckCode = GetCheckCode(entity);
            return repository.Insert(entity);
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(List<GovLogSafe> entitys)
        {
            foreach (var entity in entitys)
            {
                entity.CheckCode = GetCheckCode(entity);
            }
            return repository.Insert(entitys);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Update(GovLogSafe entity)
        {
            entity.CheckCode = GetCheckCode(entity);
            return repository.Update(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Delete(GovLogSafe entity) { return repository.Delete(entity); }

        /// <summary>
        /// 删除所有
        /// </summary>
        /// <returns></returns>
        public bool DeleteAll() { return repository.DeleteAll(); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public bool Delete(int primaryKey) { return repository.Delete(primaryKey); }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public GovLogSafe SingleOrDefault(object primaryKey) { return repository.SingleOrDefault(primaryKey); }


        /// <summary>
        /// 查询单个实体,如果存在多条则引发异常
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public GovLogSafe Single(object primaryKey) { return repository.Single(primaryKey); }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<GovLogSafe> QueryAll()
        {
            return repository.QueryAll();
        }

        #endregion

        /// <summary>
        /// 获取校验码
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public string GetCheckCode(GovLogSafe entity)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(SiteUtility.GetAppSettings("LogCheckCode"));
            sb.Append(entity.UserId);
            sb.Append(entity.Incident);
            sb.Append(entity.Source);
            sb.Append(entity.IncidentType);
            sb.Append(entity.Result);
            sb.Append(entity.Description);
            sb.Append(entity.ModuleId);
            sb.Append(entity.CreatedDate.ToString("yyyyMMddHHmmss"));
            sb.Append(entity.IsCoreFunction);
            sb.Append(entity.IsConventional);
            return MD5Helper.MD5Key(sb.ToString());
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="incidentType"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public Page<GovLogSafeDto> GetList(ScLogSafe condition)
        {
            Page<GovLogSafeDto> list = repository.GetList(condition);
            foreach (var item in list.rows)
            {
                item.CurrentCheckCode = GetCheckCode(item);
            }
            return list;
        }

        /// <summary>
        /// 导出报表
        /// </summary>
        /// <param name="condition"></param>
        public void ImportDataToExcel(ScLogSafe condition)
        {
            string[] caption = { "模块", "用户", "事件", "来源", "事件类型", "结果", "发生时间", "描述", "是否核心功能", "是否非常规业务" };
            string[] filed = { "ModuleName", "UserName", "Incident", "Source", "IncidentTypeName", "Result", "CreatedDate", "Description", "IsCoreFunctionTxt", "IsUnconventionalTxt" };
            Dictionary<int, string[]> dicRows = new Dictionary<int, string[]>();
            Page<GovLogSafeDto> list = GetList(condition);
            ExcelHelper.ExportDataSetToFile<GovLogSafeDto>(caption, list.rows, filed, "安全日志.xlsx");
        }

        /// <summary>
        /// 查询统计信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<LogSafeStatisticDto> SearchStatisticalInformation(ScLogSafe condition)
        {
            return repository.SearchStatisticalInformation(condition);
        }

        /// <summary>
        /// 当模块为非常规或核心业务时,记录安全日志
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="incident"></param>
        /// <param name="incidentType"></param>
        /// <param name="result"></param>
        /// <param name="description"></param>
        /// <param name="moduleId"></param>
        public void Info(int userId, string incident, int incidentType, string result, string description, int moduleId)
        {
            GovMenuService gmService = new GovMenuService();
            GovMenu info = gmService.GetGovMenuByCode(moduleId);
            if (info != null)
            {
                if (info.IsConventional == 0 || info.IsCoreFunction == 1)
                {
                    GovLogSafe ls = new GovLogSafe
                    {
                        UserId = userId,
                        ModuleId = moduleId,
                        CreatedDate = DateTime.Now,
                        Description = description,
                        Incident = incident,
                        IncidentType = incidentType,
                        IsConventional = info.IsConventional,
                        IsCoreFunction = info.IsCoreFunction,
                        Result = result,
                        Source = IPHelper.IPAddress
                    };
                    Insert(ls);
                }
            }

        }
    }

}